package com.gking.processHarvest.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.Example;
import com.gking.processHarvest.entity.Harvest;
import com.gking.processHarvest.service.Harvest.HarvestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/harvest")
public class HarvestController {

    @Autowired
    private HarvestService harvestService;


    /**
     * 创建 收获
     *
     * @param harvest 收获
     * @return s
     */
    @PostMapping("/create")
    public Res<String> create(@RequestBody Harvest harvest) {
        log.warn("传递过来的 harvest 信息：{}", harvest);
        return harvestService.create(
                harvest.getProjectId(), harvest.getModuleId(),
                harvest.getDescription().trim()
        );
    }

    /**
     * 删除收获
     *
     * @param id id
     * @return s
     */
    @DeleteMapping("/delete/{id}")
    public Res<String> delete(@PathVariable String id) {
        log.warn("要删除的 id：{}", id);
        return harvestService.delete(id);
    }


    /**
     * 修改收获描述
     *
     * @param harvest 收获
     * @return s
     */
    @PutMapping("/update")
    public Res<String> updateDesc(@RequestBody Harvest harvest) {
        log.warn("传递过来的更新信息为：{}", harvest);
        return harvestService.updateDesc(harvest.getId(), harvest.getDescription());
    }


    /**
     * 修改收获所属模块
     *
     * @param harvest 收获
     * @return s
     */
    @PutMapping("/modifyModule")
    public Res<String> modifyModule(@RequestBody Harvest harvest) {
        log.warn("修改的模块信息为：{}", harvest);
        return harvestService.modifyModule(harvest.getId(), harvest.getModuleId());
    }

    /**
     * 分页获取收获
     *
     * @param projectId 项目id
     * @param page      分页页码
     * @param pageSize  分页大小
     * @return s
     */
    @GetMapping("/pageList")
    public Res<Page> getPageHarvestList(String projectId, Integer page, Integer pageSize) {
        log.warn("要分页获取收获的项目 id 为：{}", projectId);
        log.warn("获取收获的分页信息 page为：{}, pageSize为：{}", page, pageSize);
        if (projectId.equals("")) return Res.error("请补充好项目id");
        if (page == null || page == 0) page = 1;
        if (pageSize == null || pageSize == 0) pageSize = 10;
        return harvestService.getPageHarvestList(projectId, page, pageSize);
    }


    /**
     * 获取收获详情
     *
     * @param harvestId 收获id
     * @return s
     */
    @GetMapping("/detail/{harvestId}")
    public Res<Harvest> getHarvestDetail(@PathVariable String harvestId) {
        log.warn("要获取收获详情的收获 id 为：{},  ", harvestId);
        if (harvestId == null || harvestId.equals("")) return Res.error("收获id不能为 0");
        return harvestService.getHarvestDetail(harvestId);
    }


    /**
     * 获取收获示例列表
     *
     * @param harvestId 收获id
     * @return s
     */
    @GetMapping("/exampleList/{harvestId}")
    public Res<List<Example>> getHarvestExamples(@PathVariable String harvestId) {
        log.warn("要获取收获示例列表的收获 id 为：{}, ", harvestId);
        if (harvestId == null || harvestId.equals("")) return Res.error("收获id不能为 0");
        return harvestService.getHarvestExamples(harvestId);
    }
}
